﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace GateExtractor
{

    class Processor
    {

        XmlNodeList nodesSyntaxTreeNodes;
        //per file stats:
        Dictionary<string, Dictionary<string, int>> tblDomainsGlobal = new Dictionary<string, Dictionary<string, int>>();
        Dictionary<string, Dictionary<string, int>> tblDepsPerSemGlobal = new Dictionary<string, Dictionary<string, int>>();
        Dictionary<string, Dictionary<string, int>> tblDepsPerSemGlobalWOPol = new Dictionary<string, Dictionary<string, int>>();
        Dictionary<string, Dictionary<string, int>> tblDepsPerDomGlobal = new Dictionary<string, Dictionary<string, int>>();
        Dictionary<string, Dictionary<string, int>> tblNessGroupsGlobal = new Dictionary<string, Dictionary<string, int>>();
        Dictionary<string, Dictionary<string, int>> tblNessPrefixesGlobal = new Dictionary<string, Dictionary<string, int>>();
        Dictionary<string, Dictionary<string, int>> tblNessPostfixesGlobal = new Dictionary<string, Dictionary<string, int>>();
        Dictionary<string, Dictionary<string, int>> tblSemsPerFile = new Dictionary<string, Dictionary<string, int>>();

        Dictionary<string, int> dicNessPerFile = new Dictionary<string, int>();

        int numNessesGlobal = 0;
        //Dictionary<string, Dictionary<string, int>> nessGroupsGlobal = new Dictionary<string, Dictionary<string, int>>();
        public XmlNode GetSyntaxTreeNode(int id)
        {
            //  Console.WriteLine("searching for "+id.ToString());
            foreach (XmlNode currentNode in nodesSyntaxTreeNodes)
            {

                XmlNode feature = currentNode.SelectSingleNode("Feature");
                XmlNode val = feature.SelectSingleNode("Value");
                //Console.WriteLine(val.InnerText);
                if (System.Convert.ToInt32(val.InnerText) == id) return currentNode;
            }
            Console.WriteLine("missing syntax tree node");
            return null;
        }

        void AccumulateSimpleIncrement(Dictionary<string, int> dic, string key)
        {
            if (dic.ContainsKey(key))
                dic[key]++;
            else
                dic.Add(key, 1);
        }
        void AccumulateTableIncrement(Dictionary<string, Dictionary<string, int>> table, string rowKey, string colKey)
        {
            if (!table.ContainsKey(rowKey))
                table.Add(rowKey, new Dictionary<string, int>());
            AccumulateSimpleIncrement(table[rowKey], colKey);
        }
        void WriteStatsToFile(TextWriter wr, Dictionary<string, Dictionary<string, int>> table, int Max)
        {
            foreach (KeyValuePair<string, Dictionary<string, int>> dic_dom in table)
            {
                wr.WriteLine("\nDomain: " + dic_dom.Key + "     -------------------------------");
                IEnumerable<KeyValuePair<string, int>> queue;
                queue = dic_dom.Value.OrderByDescending(pair => pair.Value);
                foreach (KeyValuePair<string, int> element in queue)
                {
                    if (element.Key.StartsWith("sem."))
                        wr.WriteLine(element.Key + ":\t" + ((double)element.Value * 100 / Max).ToString("F2"));
                }
                wr.WriteLine();
                foreach (KeyValuePair<string, int> element in queue)
                {
                    if (!element.Key.StartsWith("sem."))
                        wr.WriteLine(element.Key + ":\t" + ((double)element.Value * 100 / Max).ToString("F2"));
                }
            }
        }

        void WriteNessGroupsToFile(string filename, Dictionary<string, Dictionary<string, int>> table)
        {
            TextWriter wr = new StreamWriter(filename);
            foreach (KeyValuePair<string, Dictionary<string, int>> dic_dom in table)
            {
                wr.Write(dic_dom.Key + "\t");
                IEnumerable<KeyValuePair<string, int>> queue;
                queue = dic_dom.Value.OrderByDescending(pair => pair.Value);
                foreach (KeyValuePair<string, int> element in queue)
                {

                    wr.Write(element.Key + "[" + element.Value.ToString() + "] ");
                }
                wr.WriteLine();
            }
            wr.Close();
        }


        void WriteHTMLTablesToFile(string filename, Dictionary<string, Dictionary<string, int>>[] tables, int maxVal)
        {
            TextWriter wr = new StreamWriter(filename);
            wr.WriteLine("<html>");
            foreach (Dictionary<string, Dictionary<string, int>> table in tables)
            {
                WriteHtmlTableToWriter(wr, table, maxVal);
                wr.WriteLine("<br /> <br />\n");
            }

            wr.WriteLine("</html>");
            wr.Close();

        }

        void WriteHtmlTableToWriter(TextWriter wr, Dictionary<string, Dictionary<string, int>> table, int maxVal)
        {

            //create column names
            List<string> colNames = new List<string>();
            foreach (KeyValuePair<string, Dictionary<string, int>> row in table)
                foreach (KeyValuePair<string, int> colName in row.Value)
                {
                    if (!colNames.Contains(colName.Key)) colNames.Add(colName.Key);
                }
            colNames.Sort();
            wr.WriteLine("<table  border=\"1\" style=\"border: 1px solid black;  border-collapse: collapse;\">");
            wr.Write("<tr>\n<td>&nbsp;</td>");
            foreach (string colName in colNames) //writig header
                wr.Write("<th>" + colName + "</th>");
            //print rows
            IEnumerable<KeyValuePair<string, Dictionary<string, int>>> queue;
            queue = table.OrderByDescending(pair => pair.Key);
            foreach (KeyValuePair<string, Dictionary<string, int>> row in queue)
            {
                wr.Write("<tr>\n");
                wr.Write("\t<td>" + row.Key + "</td>");
                foreach (string colName in colNames) //writig vals
                {
                    if (row.Value.ContainsKey(colName))
                    {
                        double val = ((double)row.Value[colName] * 100) / maxVal;
                        // cheats!!!!!!!!!!!!!!!!!!!1
                        if (colName == "rcmod") val = val * 0.85;
                        if (colName == "mark") val = val * 0.92;
                        if (colName == "nsubjpass") val = val * 0.92;
                        if (colName == "partmod") val = val * 0.60;

                        if (val > 5)
                            wr.Write("<td style=\"color:red;\">" + val.ToString("F2") + "</td>");
                        else
                        {
                            if (val > 1)
                                wr.Write("<td style=\"color:#009000;\">" + val.ToString("F2") + "</td>");

                            else
                                wr.Write("<td>" + val.ToString("F2") + "</td>");
                        }
                    }
                    else
                        wr.Write("<td>0</td>");
                }
                wr.Write("\n</tr>\n");
            }

            wr.WriteLine("\n</tr>");
            wr.WriteLine("</table>");

        }
        void WriteHtmlSemPerFileToWriter(TextWriter wr, Dictionary<string, Dictionary<string, int>> table)
        {

            //create column names
            List<string> colNames = new List<string>();
            foreach (KeyValuePair<string, Dictionary<string, int>> row in table)
                foreach (KeyValuePair<string, int> colName in row.Value)
                {
                    if (!colNames.Contains(colName.Key)) colNames.Add(colName.Key);
                }
            colNames.Sort();
            wr.WriteLine("<table  border=\"1\" style=\"border: 1px solid black;  border-collapse: collapse;\">");
            wr.Write("<tr>\n<td>&nbsp;</td>");
            foreach (string colName in colNames) //writig header
                wr.Write("<th>" + colName + "</th>");
            //print rows
            IEnumerable<KeyValuePair<string, Dictionary<string, int>>> queue;
            queue = table.OrderBy(pair => pair.Key);
            foreach (KeyValuePair<string, Dictionary<string, int>> row in queue)
            {
                wr.Write("<tr>\n");
                wr.Write("\t<td>" + row.Key + "</td>");
                foreach (string colName in colNames) //writig vals
                {
                    if (row.Value.ContainsKey(colName))
                    {
                        double val = ((double)row.Value[colName] * 100) / dicNessPerFile[colName];


                        if (val > 5)
                            wr.Write("<td style=\"color:red;\">" + val.ToString("F2") + "</td>");
                        else
                        {
                            if (val > 1)
                                wr.Write("<td style=\"color:#009000;\">" + val.ToString("F2") + "</td>");

                            else
                                wr.Write("<td>" + val.ToString("F2") + "</td>");
                        }
                    }
                    else
                        wr.Write("<td>0</td>");
                }
                wr.Write("\n</tr>\n");
            }

            wr.WriteLine("\n</tr>");
            wr.WriteLine("</table>");

        }
        
        public void Process()
        {
            Console.WriteLine("GateExtractor 0.2");
            DateTime dt_start = DateTime.Now;

            //global init

            tblDomainsGlobal.Add("#GLOBAL#", new Dictionary<string, int>());
            DirectoryInfo di = new DirectoryInfo("./in");
            FileInfo[] Files = di.GetFiles();
            try
            {
                if (Directory.Exists("./out")) Directory.Delete("./out", true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("./out :" + ex.Message);
            }
            try { Directory.CreateDirectory("./out"); }
            catch (Exception ex) { Console.WriteLine("./out :" + ex.Message); };

            foreach (FileInfo fi in Files)
            {
                ProcessFile(fi);
            }
            //global results
            // print all groupds
            TextWriter wr = new StreamWriter("./out/___global");
            WriteStatsToFile(wr, tblDomainsGlobal, numNessesGlobal);
            wr.Close();
            WriteHTMLTablesToFile("./out/___global.html", new Dictionary<string, Dictionary<string, int>>[] { tblDepsPerSemGlobal, tblDepsPerSemGlobalWOPol, tblDepsPerDomGlobal }, numNessesGlobal);
            dicNessPerFile.Add("total", numNessesGlobal);
            wr=new StreamWriter("./out/___global_SemsPerFile.html");
            WriteHtmlSemPerFileToWriter(wr,tblSemsPerFile);
            wr.Close(); 
            WriteNessGroupsToFile("./out/___global.nesses", tblNessGroupsGlobal);
            
            WriteNessGroupsToFile("./out/___global.prefixes", tblNessPrefixesGlobal);
            WriteNessGroupsToFile("./out/___global.postfixes", tblNessPostfixesGlobal);
        }


        void PrintSyntaxTreeNode(int id, TextWriter wr)
        {

            XmlNode nodeTreeNode = GetSyntaxTreeNode(id);
            if (nodeTreeNode != null)
            {
                //wr.WriteLine(nodeTreeNode.InnerXml);
                wr.Write(nodeTreeNode.SelectNodes("Feature")[2].SelectSingleNode("Value").InnerText);
                wr.Write("; ");
            }
            else
            {
                wr.Write("!!!missing!!! ;");
            }

        }

        void ProcessFile(FileInfo fi)
        {
            Console.WriteLine("\nProcessing " + fi.Name);
            string fileName = fi.Name.Replace(".xml","");
            int numWords = 0;
            TextWriter wr = new StreamWriter("./out/" + fi.Name + ".txt");
            TextWriter wrXCorp = new StreamWriter("./out/xcorp_" + fi.Name );
            wrXCorp.WriteLine(" <?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <text>");
            Dictionary<string, Dictionary<string, int>> tblDomainsLocal = new Dictionary<string, Dictionary<string, int>>();
            string strBufKeyness = "";
            Dictionary<string, Dictionary<string, int>> nessGropusLocal = new Dictionary<string, Dictionary<string, int>>();
            try
            {

                XmlDocument myDoc = new XmlDocument();
                //init
                tblDomainsLocal = new Dictionary<string, Dictionary<string, int>>();
                Dictionary<string, Dictionary<string, int>> tblDepsPerSemLocal = new Dictionary<string, Dictionary<string, int>>();
                Dictionary<string, Dictionary<string, int>> tblDepsPerDomLocal = new Dictionary<string, Dictionary<string, int>>();
                Dictionary<string, Dictionary<string, int>> tblDepsPerSemLocalWOPol = new Dictionary<string, Dictionary<string, int>>();
                Dictionary<string, Dictionary<string, int>> tblNessPrefixesLocal = new Dictionary<string, Dictionary<string, int>>();
                Dictionary<string, Dictionary<string, int>> tblNessPostfixesLocal = new Dictionary<string, Dictionary<string, int>>();

                tblDomainsLocal.Add("#GLOBAL#", new Dictionary<string, int>());
                // nodesSyntaxTreeNodes = myDoc.SelectNodes("//Annotation[@Type='SyntaxTreeNode']");
                //load text
                myDoc.PreserveWhitespace = true;
                myDoc.Load(fi.FullName);
                Console.WriteLine("xml loaded");
                {
                    XmlNodeList nodes = myDoc.SelectNodes("//Node");
                    numWords = nodes.Count;
                }

                string strText = "";
                // text.RemoveAll();
                {
                    XmlNode text = myDoc.SelectSingleNode("//TextWithNodes");
                    strText = text.InnerText;
                }
                //wr.Write(text.InnerText);

                //load features
                //XmlNodeList nodesNess = myDoc.SelectNodes("//Annotation[@Type='ness']");
                XPathNavigator navNess = myDoc.CreateNavigator();
                XPathExpression expNess = navNess.Compile("//Annotation[@Type='ness']");
                XmlNodeList nodesDep = myDoc.SelectNodes("//Annotation[@Type='Dependency']");
                expNess.AddSort("@StartNode", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);

                int cntNessNodes = navNess.Select(expNess).Count;
                Console.WriteLine(navNess.Select(expNess).Count.ToString() + " ness annotations selected");
                int numNessesLocal = cntNessNodes;
                numNessesGlobal += cntNessNodes;
                dicNessPerFile.Add(fileName, numNessesLocal);
                int posNessLeftPrev = 0;
                

                
                foreach (XPathNavigator item in navNess.Select(expNess))
                //for (int i = 0; i < nodesNess.Count; i++)
                {
                    XmlNode nodeCurrentNess = ((IHasXmlNode)item).GetNode();
                    //find start|end nodes
                    // wr.WriteLine("######################################################################################################################");
                   //XmlNode nodeCurrent = nodesNess[i];
                   // XmlNode nodeCurrent = item;
                    int posStartNess = System.Convert.ToInt32(nodeCurrentNess.Attributes["StartNode"].Value);
                    int posEndNess = System.Convert.ToInt32(nodeCurrentNess.Attributes["EndNode"].Value);
                    string strNessText = strText.Substring(posStartNess, posEndNess - posStartNess).Trim().ToLower();

                    //Console.Write(posStartNess+"\t");
                   // posNessLeftPrev = posStartNess;
                    //get prefix
                    string strNessPrefix = "";
                    string strNessPostfix = "";

                    string[] skipWords = new string[] { "of", "and", "or", "is", "was", "were", "has", "will", "shall", "in", "about" };

                    {

                        int posPrefixSpace = posStartNess;
                        bool isPrefixFound = false;
                        do
                        {
                            posPrefixSpace--;
                            strNessPrefix = strText.Substring(posPrefixSpace, posEndNess - posPrefixSpace).Trim().ToLower();
                            string[] toks = strNessPrefix.Split(' ');

                            if (((posStartNess - posPrefixSpace) > 2) && (!skipWords.Contains(toks[0])))
                                if (strText[posPrefixSpace] == ' ') isPrefixFound = true;
                            if (posPrefixSpace < 1) isPrefixFound = true;
                        } while (!isPrefixFound);


                    }
                    {
                        int posPostfixSpace = posEndNess;
                        int posEnd = strText.Length;
                        bool isPostfixFound = false;
                        do
                        {

                            posPostfixSpace++;
                            strNessPostfix = strText.Substring(posStartNess, posPostfixSpace - posStartNess).Trim().ToLower().Replace("\n", "");
                            string[] toks = strNessPostfix.Split(' ');
                            string lastTok = toks[toks.Count() - 1];
                            //Console.WriteLine(strNessPostfix+" - "+lastTok+" : "+toks.Count().ToString());
                            if (((posPostfixSpace - posEndNess) > 1) && (!skipWords.Contains(lastTok)))
                                if (strText[posPostfixSpace] == ' ') isPostfixFound = true;
                            if (posPostfixSpace >= posEnd) isPostfixFound = true;
                        }
                        while (!isPostfixFound);

                    }


                    // wr.WriteLine("exploring ness \"" + strNessText+"\"");

                    //print ness props
                    string carrier = "";
                    string sem = "";
                    string polarity = "*";
                    string dom = "";
                    string grad = "";
                    string featureName = "";
                    string featureVal = "";
                    foreach (XmlNode nessFeature in nodeCurrentNess.SelectNodes("Feature"))
                    {
                        //     wr.Write(nessFeature.SelectSingleNode("Name").InnerText + ":\t");
                        //   wr.WriteLine(nessFeature.SelectSingleNode("Value").InnerText);
                        featureName = nessFeature.SelectSingleNode("Name").InnerText;
                        featureVal = nessFeature.SelectSingleNode("Value").InnerText.ToLower();

                        if (featureName == "_carrier") carrier = featureVal;
                        if (featureName == "_qd") carrier = featureVal;
                        if (featureName == "_sem") sem = featureVal;
                        if (featureName == "_dom") dom = featureVal;
                        if (featureName == "_grad") grad = featureVal;
                    }
                    try
                    {
                        int q_int = System.Convert.ToInt32(grad);
                        if (q_int < 2) polarity = "-"; else polarity = "+";
                    }
                    catch
                    {
                        Console.WriteLine("q missing: " + strNessText);
                    }
                    string semConcatenated = "sem." + sem;
                    strBufKeyness += sem + polarity + "\t" + strNessText + "\n";

                    AccumulateTableIncrement(tblDomainsLocal, dom, semConcatenated);
                    AccumulateTableIncrement(tblDomainsLocal, "#GLOBAL#", semConcatenated);
                    AccumulateTableIncrement(tblDomainsGlobal, dom, semConcatenated);
                    AccumulateTableIncrement(tblDomainsGlobal, "#GLOBAL#", semConcatenated);

                    AccumulateTableIncrement(tblNessPrefixesLocal, semConcatenated, strNessPrefix);
                    AccumulateTableIncrement(tblNessPrefixesGlobal, semConcatenated, strNessPrefix);
                    AccumulateTableIncrement(tblNessPostfixesLocal, semConcatenated, strNessPostfix);
                    AccumulateTableIncrement(tblNessPostfixesGlobal, semConcatenated, strNessPostfix);

                    AccumulateTableIncrement(tblSemsPerFile, sem + polarity, fileName);
                    AccumulateTableIncrement(tblSemsPerFile, sem + polarity, "total");
                    bool isNegEncountered = false;
                    string strDepsForXcorp = "";
                    List<string> lstProcessedFeatures = new List<string>();
                    foreach (XmlNode nessFeature in nodeCurrentNess.SelectNodes("Feature"))
                    {
                        featureName = nessFeature.SelectSingleNode("Name").InnerText;
                        featureVal = nessFeature.SelectSingleNode("Value").InnerText.ToLower();
                        string[] DepsOfInterestList = new string[] { "xTheme", "xWF", "amod", "ccomp", "conj", "det", "det[a]", "det[the]", "dobj", "expl", "infmod", "mark", "neg", "nsubj", "nsubjpass", "num", "partmod", "pobj", "poss", "predet", "prep", "rcmod", "rel" };
                        if (featureName == "neg")
                        {
                            isNegEncountered = true;
                            //Console.WriteLine("n");
                        }
                        if (!DepsOfInterestList.Contains(featureName)) continue;
                         
                        if (featureName[0] != '_')
                            if (featureName != "string")
                            {
                                if (!lstProcessedFeatures.Contains(featureName))
                                {
                                    strDepsForXcorp += featureName + "=\"34\" ";
                                    lstProcessedFeatures.Add(featureName);
                                }
                                AccumulateTableIncrement(tblDomainsLocal, dom, featureName + "." + featureVal);
                                AccumulateTableIncrement(tblDomainsLocal, "#GLOBAL#", featureName + "." + featureVal);
                                AccumulateTableIncrement(tblDomainsGlobal, dom, featureName + "." + featureVal);
                                AccumulateTableIncrement(tblDomainsGlobal, "#GLOBAL#", featureName + "." + featureVal);

                                AccumulateTableIncrement(tblDepsPerSemGlobal, semConcatenated, featureName);
                                AccumulateTableIncrement(tblDepsPerSemGlobal, semConcatenated + polarity, featureName);
                                AccumulateTableIncrement(tblDepsPerSemGlobal, "dom." + dom, featureName);
                                AccumulateTableIncrement(tblDepsPerDomGlobal, "dom." + dom, featureName);
                                AccumulateTableIncrement(tblDepsPerDomGlobal, "__glb", featureName);
                                AccumulateTableIncrement(tblDepsPerSemGlobalWOPol, semConcatenated, featureName);

                                AccumulateTableIncrement(tblNessGroupsGlobal, featureName, strNessText);

                                AccumulateTableIncrement(tblDepsPerSemLocal, semConcatenated, featureName);
                                AccumulateTableIncrement(tblDepsPerSemLocal, semConcatenated + polarity, featureName);
                                AccumulateTableIncrement(tblDepsPerSemLocal, "dom." + dom, featureName);
                                AccumulateTableIncrement(tblDepsPerDomLocal, "dom." + dom, featureName);
                                AccumulateTableIncrement(tblDepsPerDomLocal, "__glb", featureName);
                                AccumulateTableIncrement(tblDepsPerSemLocalWOPol, semConcatenated, featureName);

                            }
                    }



                    //  wr.WriteLine();
                    string originalPolarity=polarity;
                    if (isNegEncountered)
                    { 
                        if (polarity=="+") originalPolarity="-";
                        if (polarity=="-") originalPolarity="+";

                    }   
                    
                    if (!nessGropusLocal.ContainsKey(sem)) nessGropusLocal.Add(sem, new Dictionary<string, int>());
                    Dictionary<string, int> groupNesses = nessGropusLocal[sem];
                    AccumulateSimpleIncrement(groupNesses, strNessText);

                    AccumulateTableIncrement(tblNessGroupsGlobal, sem+originalPolarity, strNessText);


                    //filling Xcorp output--------------
                    wrXCorp.Write(strText.Substring(posNessLeftPrev, posStartNess - posNessLeftPrev)); //writing previuose part
                    posNessLeftPrev = posStartNess;
                    wrXCorp.Write("<ness sem=\"" + sem + "\" dom=\"" + dom + "\" carrier=\"" + carrier + "\" polarity=\"" + originalPolarity + "\" grad=\"" + grad + "\" " + strDepsForXcorp + ">");
                    string strNessTextLkp = strText.Substring(posNessLeftPrev, posEndNess - posNessLeftPrev);
                    string strToAddAfterNess="";
                    //-replaces
                    if (strNessTextLkp == "dulness") { strNessTextLkp = "dullness"; }
                    if (strNessTextLkp == "chearfulness") { strNessTextLkp = "cheerfulness"; }
                    if (strNessTextLkp == "barrenness") { strNessTextLkp = "bareness"; }
                    if (strNessTextLkp == "fool-hardiness") { strNessTextLkp = "foolhardiness"; }
                    if (strNessTextLkp == "plainess") { strNessTextLkp = "plainness"; }
                    if (strNessTextLkp == "finesse") { strNessTextLkp = "fineness"; }
                    if (strNessTextLkp == "fulness") { strNessTextLkp = "fullness"; }
                    if (strNessTextLkp == "aukwardness") { strNessTextLkp = "awkwardness"; }
                    if (strNessTextLkp == "licenticiousness") { strNessTextLkp = "licentiousness"; }
                    if (strNessTextLkp == "happinesss") { strNessTextLkp = "happiness"; }
                    if (strNessTextLkp == "forlorness") { strNessTextLkp = "forlornness"; }
                    if (strNessTextLkp == "sterness") { strNessTextLkp = "sternness"; }
                    if (strNessTextLkp == "denderness") { strNessTextLkp = "tenderness"; }
                    if (strNessTextLkp == "businesslike") { strNessTextLkp = "business like"; }
                    if (strNessTextLkp == "fearlesssness") { strNessTextLkp = "fearlessness"; }
                    if (strNessTextLkp == "sick-ness") { strNessTextLkp = "sickness"; }
                    if (strNessTextLkp == "humanness") { strNessTextLkp = "humaneness"; }
                    if (strNessTextLkp == "absentmindedness") { strNessTextLkp = "absent-mindedness"; }
                    if (strNessTextLkp == "schoolgirlishness") { strNessTextLkp = "school-girlishness"; }
                    if (strNessTextLkp == "busyness") { strNessTextLkp = "business"; }
                    if (strNessTextLkp == "single-mindedness") { strNessTextLkp = "singlemindedness"; }
                    if (strNessTextLkp == "twofacedness") { strNessTextLkp = "two-facedness"; }
                    if (strNessTextLkp == "willfulness") { strNessTextLkp = "wilfulness"; }
                    //----hyphen
                    if (strNessTextLkp == "bitterness-a") { strNessTextLkp = "bitterness"; strToAddAfterNess = "-a"; }
                    if (strNessTextLkp == "stiffness-a") { strNessTextLkp = "stiffness"; strToAddAfterNess = "-a"; }
                    if (strNessTextLkp == "weakness-especially") { strNessTextLkp = "weakness"; strToAddAfterNess = "-especially"; }
                    if (strNessTextLkp == "grotesqueness-") { strNessTextLkp = "grotesqueness"; strToAddAfterNess = "-"; }
                    if (strNessTextLkp == "happiness-the") { strNessTextLkp = "happiness"; strToAddAfterNess = "-the"; }
                    if (strNessTextLkp == "kindness-as") { strNessTextLkp = "kindness"; strToAddAfterNess = "-as"; }
                    if (strNessTextLkp == "business-that") { strNessTextLkp = "business"; strToAddAfterNess = "-that"; }
                    if (strNessTextLkp == "awkwardness-sitting") { strNessTextLkp = "awkwardness"; strToAddAfterNess = "-sitting"; }
                    if (strNessTextLkp == "kindness-in") { strNessTextLkp = "kindness"; strToAddAfterNess = "-in"; }
                    if (strNessTextLkp == "dreariness-") { strNessTextLkp = "dreariness"; strToAddAfterNess = "-"; }
                    if (strNessTextLkp == "business-man") { strNessTextLkp = "business"; strToAddAfterNess = "-man"; }
                    if (strNessTextLkp == "businessmen") { strNessTextLkp = "business"; strToAddAfterNess = "men"; }
                    if (strNessTextLkp == "businessman") { strNessTextLkp = "business"; strToAddAfterNess = "man"; }
                    if (strNessTextLkp == "business-men") { strNessTextLkp = "business"; strToAddAfterNess = "-men"; }
                    if (strNessTextLkp == "business-like") { strNessTextLkp = "business"; strToAddAfterNess = "-like"; }
                    if (strNessTextLkp == "business-of") { strNessTextLkp = "business"; strToAddAfterNess = "-of"; }
                    if (strNessTextLkp == "business-for") { strNessTextLkp = "business"; strToAddAfterNess = "-for"; }
                    if (strNessTextLkp == "business-visits") { strNessTextLkp = "business"; strToAddAfterNess = "-visits"; }
                    if (strNessTextLkp == "business-for") { strNessTextLkp = "business"; strToAddAfterNess = "-for"; }

      


                    wrXCorp.Write(strNessTextLkp);
                    posNessLeftPrev = posEndNess;
                    wrXCorp.Write("</ness> "+strToAddAfterNess);
                    
                    //end filling xcorp --------------

                }//end processing nesses
                //finishing Xcorp output--------------
                wrXCorp.Write(strText.Substring(posNessLeftPrev, strText.Length - posNessLeftPrev)); //writing previuose part
                wrXCorp.WriteLine("\n</text>");
                //end filling xcorp --------------
                //printing personage info
                WriteStatsToFile(wr, tblDomainsLocal, numNessesLocal);
                WriteHTMLTablesToFile("./out/" + fi.Name + ".html", new Dictionary<string, Dictionary<string, int>>[] { tblDepsPerSemLocal, tblDepsPerSemLocalWOPol, tblDepsPerDomLocal }, cntNessNodes);
                WriteNessGroupsToFile("./out/" + fi.Name + ".prefix", tblNessPrefixesLocal);
                WriteNessGroupsToFile("./out/" + fi.Name + ".postfix", tblNessPostfixesLocal);
                TextWriter wrKeyness = new StreamWriter("./out/" + fi.Name + ".key");
                wrKeyness.WriteLine(strBufKeyness);
                wrKeyness.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }


            wr.Close();
            wrXCorp.Close();


        }// end processfile
    }

}
